package map
{
	import com.game.res.UIRescource;
	
	import fl.core.UIComponent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	public class SceneMap extends UIComponent {
		
		private var bit:Bitmap = new Bitmap;
		private var shap:Shape = new Shape;
		
		public static const CELL_WIDTH:int = 40;
		public static const CELL_HEIGHT:int = 40;
		
		public var wCnt:int = 10;
		public var hCnt:int = 10;
		private var _showRect:Rectangle;
		
		private var _cache:Object ={};
		protected var mapId:int =0;
		
		private var dic:Dictionary = new Dictionary;
		private var needRedrow:Boolean = true;
		
		public function SceneMap() {
			super();
			_showRect = new Rectangle(0,0,wCnt*CELL_WIDTH,hCnt*CELL_HEIGHT)
			bit.bitmapData = new BitmapData(wCnt*CELL_WIDTH,hCnt*CELL_HEIGHT,false,0);
			addChild(bit);
			
			addChild(shap);
		}
		
		public function drawGeZi():void{
			shap.graphics.clear();
			shap.graphics.lineStyle(1,0xFFFFFF,1);
			var rowCnt:int = Math.ceil(this.width/CELL_WIDTH);
			var collowCnt:int = Math.ceil(this.height/CELL_HEIGHT);
				
			for (var x:int =0 ;x<rowCnt ;x++){
				shap.graphics.moveTo(x*CELL_WIDTH,0);
				shap.graphics.lineTo(x*CELL_WIDTH,this.height);
			}
			
			for (var y:int =0 ;y<collowCnt ;y++){
				shap.graphics.moveTo(0,y*CELL_HEIGHT);
				shap.graphics.lineTo(this.width,y*CELL_HEIGHT);
			}
		}
		
		public function fillCellColor(row:int,collow:int,color:uint, alpha:Number=1.0):void{
			shap.graphics.beginFill(color,alpha);
			shap.graphics.drawRect(row*CELL_WIDTH,collow*CELL_HEIGHT,CELL_WIDTH,CELL_HEIGHT);
			shap.graphics.endFill();
		}
		
		public function getCell(x:int,y:int):Point{
			return new Point(int(x/CELL_WIDTH),int(y/CELL_HEIGHT));
		}
		
		public function updateCenter(nx:int,ny:int):void{
			updateShowRect(nx+_showRect.width/2,ny+_showRect.height/2);
		}
		public function updateShowRect(nx:int,ny:int):void{
			
			if(int(_showRect.x/CELL_WIDTH) ==int(nx/CELL_WIDTH)){
				_showRect.x = int(nx/CELL_WIDTH)*CELL_WIDTH;
				bit.x = (_showRect.x-nx);
			}else{
				_showRect.x = int(nx/CELL_WIDTH)*CELL_WIDTH;
				bit.x = (int(nx/CELL_WIDTH)*CELL_WIDTH-nx);
				needRedrow= true;
			}
			
			if(int(_showRect.y/CELL_HEIGHT) ==int(ny/CELL_HEIGHT)){
				_showRect.y = int(ny/CELL_HEIGHT)*CELL_HEIGHT;
				bit.y = (_showRect.y-ny);
			}else{
				_showRect.y = int(ny/CELL_HEIGHT)*CELL_HEIGHT;
				bit.y = (int(ny/CELL_HEIGHT)*CELL_HEIGHT-ny);
				needRedrow= true;
			}
			
			if(	needRedrow== true){
				needRedrow = false;
				redraw();
			}
			trace("_showRect",_showRect.x,_showRect.y);
		}
		
		private function redraw():void{
			for (var i:int =0;i<hCnt*wCnt ;i++){
				var cell:MapTileEvent = _cache[i] as MapTileEvent;
				var mapTileEvent:MapTileEvent = new MapTileEvent(MapTileEvent.REQUIRE_MAP_DATA)
				mapTileEvent.mapId = mapId;
				mapTileEvent.tilex = _showRect.x + i%wCnt*CELL_WIDTH;
				mapTileEvent.tiley = _showRect.y + int(i/wCnt)*CELL_HEIGHT;
				dispatchEvent(mapTileEvent);
			}
		}
		
		public function setMapCell(mapTileEvent:MapTileEvent):void{
			if(mapId != mapTileEvent.mapId){
				return;
			}
			var index:int = 0;
			index = int((mapTileEvent.tilex-_showRect.x)/CELL_WIDTH)+wCnt*int((mapTileEvent.tiley-_showRect.y)/CELL_HEIGHT)
			if(index < 0 || index >hCnt*wCnt){
				return;
			}
			_cache[index] = mapTileEvent;
			
			var matrix:Matrix = new Matrix();
			matrix.translate(int((mapTileEvent.tilex-_showRect.x)/CELL_WIDTH)*CELL_WIDTH,
				int((mapTileEvent.tiley-_showRect.y)/CELL_HEIGHT)*CELL_HEIGHT);
			bit.bitmapData.draw(mapTileEvent.bitmapData,matrix);
		
		}
		
		public function localToMap(point:Point):Point{
			return new Point(_showRect.x+point.x,_showRect.y+point.y);
		}
		
		public function MapTolocal(point:Point):Point{
			return new Point(point.x-_showRect.x,point.y-_showRect.y);
		}
	}
}